#include "format.h"
#include "common.h"

Format::Format()
{
    /*初始化默认程序*/
    type = "LCD";
}

bool Format::isCabPosRepeated(int screen_id, int partition_id, int layout_id, int item_id, QString cab_pos_item_str)
{
    for (int i = 0; i < screens[screen_id].partitions[partition_id].layouts[layout_id].items.size(); i++)
    {
        QList<Item> list = screens[screen_id].partitions[partition_id].layouts[layout_id].items;
        if (list.at(i).id == item_id)
        {
            if (list.at(i).cabpositems.contains(cab_pos_item_str))
                return true;
        }
    }
    return false;
}
bool Format::isIdRepeated(int new_id)
{
    for (auto it = screens.begin(); it != screens.end(); it++)
    {
        if (it.key() == new_id)
            return true;
    }
    return false;
}
int Format::getNewScreenId()
{
    int max_id = 0;
    for (auto it = screens.begin(); it != screens.end(); it++)
    {
        if (it.key() > max_id)
            max_id = it.key();
    }
    return max_id + 1;
}
int Format::getNewPartitionId(int screen_id)
{
    int max_id = 0;
    for (auto it = screens[screen_id].partitions.begin(); it != screens[screen_id].partitions.end(); it++)
    {
        if (it.key() > max_id)
            max_id = it.key();
    }
    return max_id + 1;
}
int Format::getNewLayoutId(int screen_id, int partition_id)
{
    int max_id = 0;
    for (auto it = screens[screen_id].partitions[partition_id].layouts.begin(); it != screens[screen_id].partitions[partition_id].layouts.end(); it++)
    {
        if (it.key() > max_id)
            max_id = it.key();
    }
    return max_id + 1;
}
int Format::getNewItemId(int screen_id, int partition_id, int layout_id)
{
    int max_id = 0;
    for (int i = 0; i < screens[screen_id].partitions[partition_id].layouts[layout_id].items.size(); i++)
    {
        if (screens[screen_id].partitions[partition_id].layouts[layout_id].items.at(i).id > max_id)
            max_id = screens[screen_id].partitions[partition_id].layouts[layout_id].items.at(i).id;
    }
    return max_id + 1;
}

void Format::initDeflultData(int width, int height)
{
    /*path_prefix*/
    if (type == "LCD")
    {
        path_refix.file = "/app/bin/format_manage/lcd";
        path_refix.url  = "ftp://root:hq123@(#local_ip#)/app/bin/format_manage/lcd/";
    } else
    {
        path_refix.file = "/app/bin/format_manage/idu";
        path_refix.url  = "ftp://root:hq123@(#local_ip#)/app/bin/format_manage/idu/";
    }

    /*video_stream*/
    VideoStream one_stream;
    one_stream.stream_destination = 1;
    one_stream.local              = "(#playlist1#)";
    Live one_live;
    one_live.ip            = "224.1.2.3";
    one_live.port          = 1234;
    one_live.interface     = 1;
    one_stream.lives.push_back(one_live);

    video_streams.push_back(one_stream);

    /*architecture_version*/
    architecture_version = "";

    /*current_show*/
    current_show = "(#current_show#)";

    /*resolution*/
    resolution.width  = width;
    resolution.height = height;

    /*screen1*/
    Partition one_partition1 = initPartition(1);
    one_partition1.common.width = width;
    one_partition1.common.height = height;

    Screen one_screen1 = initScreen(1);
    one_screen1.partitions[one_partition1.id] = one_partition1;
    screens[one_screen1.id] = one_screen1;

    /*screen10*/
    Item one_item10 = initItem(1);
    one_item10.cabpositems["cab[*]-position[*]"] = initCabPosItem("image", 1);
    one_item10.cabpositems["cab[*]-position[*]"].width = width;
    one_item10.cabpositems["cab[*]-position[*]"].height = height;

    Layout one_layout10 = initLayout(1);
    one_layout10.items.push_back(one_item10);

    Partition one_partition10 = initPartition(1);
    one_partition10.common.width = width;
    one_partition10.common.height = height;
    one_partition10.layouts[1] = one_layout10;

    Screen one_screen10 = initScreen(10);
    one_screen10.partitions[one_partition10.id] = one_partition10;
    screens[one_screen10.id] = one_screen10;

    /*screen11*/
    Item one_item11 = initItem(1);
    one_item11.cabpositems["cab[*]-position[*]"] = initCabPosItem("text", 1);
    one_item11.cabpositems["cab[*]-position[*]"].width = width;
    one_item11.cabpositems["cab[*]-position[*]"].height = height;

    Layout one_layout11 = initLayout(1);
    one_layout11.items.push_back(one_item11);

    Partition one_partition11 = initPartition(1);
    one_partition11.common.width = width;
    one_partition11.common.height = height;
    one_partition11.layouts[1] = one_layout11;

    Screen one_screen11 = initScreen(11);
    one_screen11.partitions[one_partition11.id] = one_partition11;
    screens[one_screen11.id] = one_screen11;
}

Screen Format::initScreen(int screen_id)
{
    Screen one_screen;
    one_screen.id = screen_id;
    if (screen_id == 1)
        one_screen.name = screen_type_list.at(0);
    if (screen_id == 10)
        one_screen.name = screen_type_list.at(1);
    if (screen_id == 11)
        one_screen.name = screen_type_list.at(2);

    return one_screen;
}

Partition Format::initPartition(int partition_id)
{
    Common common;
    common.x = 0;
    common.y = 0;
    common.width = g_pro_data.width;
    common.height = g_pro_data.height;
    common.z_order = partition_id-1;
    common.background = "";
    common.background_color = "";
    common.transparent = false;

    Partition one_partition;
    one_partition.id = partition_id;
    one_partition.common = common;

    return one_partition;
}

Layout Format::initLayout(int layout_id)
{
    Layout one_layout;
    one_layout.id = layout_id;
    one_layout.duration = 0;

    return one_layout;
}

Item Format::initItem(int item_id)
{
    Item one_item;
    one_item.id = item_id;

    return one_item;
}

CabPosItem Format::initCabPosItem(QString item_type, int item_id)
{
    Data data;

    if (item_type != "video")
    {
        Material one_material;
        one_material.id = 1;
        one_material.duration = 0;
        one_material.content  = "";
        data.materials[one_material.id] = one_material;
    } else
    {
        data.stream_index.all = 1;
    }

    CabPosItem one_cab_pos_item;
    one_cab_pos_item.id = item_id;
    one_cab_pos_item.type = item_type;
    one_cab_pos_item.x = 0;
    one_cab_pos_item.y = 0;
    one_cab_pos_item.width = g_pro_data.width;
    one_cab_pos_item.height = g_pro_data.height;

    if (item_type == "text")
    {
        one_cab_pos_item.font = "微软雅黑";
        one_cab_pos_item.size = 35;
        one_cab_pos_item.bold = true;
        one_cab_pos_item.italic = false;
        one_cab_pos_item.fore_color = "000000";
        one_cab_pos_item.back_color = "";
        one_cab_pos_item.background = "";
        one_cab_pos_item.background_color = "";
        one_cab_pos_item.align = 1;
        one_cab_pos_item.effect = 0;
        one_cab_pos_item.speed = 1;
    }
    if (item_type == "video")
        one_cab_pos_item.sound = true;
    one_cab_pos_item.data = data;

    return one_cab_pos_item;
}
